package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.cim.ledger.excetpion.ManagementUnitException;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.ledger.services.ManagementUnitService;
import com.sduept.cim.model.core.Substation;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.nwld.dataserver.manager.protection.ProtectOverhaulManager;
import com.sduept.nwld.dataserver.model.daily.OverhaulMsg;
import com.sduept.utils.PingYinUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;

@Named
@ViewScoped
public class SubstationCompelteController extends AbstractController {

	/**
	 * 保护检修管理controller类
	 */
	private static final long serialVersionUID = 4797459228239626554L;

	@Autowired
	private MapDataCache cim;

	@Autowired
	private ManagementUnitService unitM;
	@Autowired
	private ProtectOverhaulManager ma;
	

	public Map<String, List<String>> applicantCompanysMap = new HashMap<>();
	public Map<String, List<String>> cimstation = new HashMap<>();
	private List<Substation> substations = new ArrayList<>();
	private List<String> applicantCompanys = new ArrayList<>();
	private List<String> subareas = new ArrayList<>();//调管范围
	public List<String> rankList = new ArrayList<String>();//检修范围

	@PostConstruct
	private void init() {
		substations = cim.getS500List();
		initApplicantCompanys();
		for (Substation r : substations) {
			String key = PingYinUtil.getPingYinHeader(r.getName());
			List<String> ss = cimstation.get(key);
			if (ss == null) {
				ss = new ArrayList<>();
				cimstation.put(key, ss);
			}
			ss.add(r.getName());
		}
		//初始化调管范围
//		ManagementUnitDict unit = unitM.getRootUnit(ManagementUnitEnum.DESORG.getType());
//		List<ManagementUnitDict> subs = unitM.getUnitsByPid(unit.getId());
		List<ManagementUnitDict> depts;
		try {
			depts = unitM.getAllDispatchingManagements();
			for (ManagementUnitDict i : depts) {
				subareas.add(i.getName());
			}
		} catch (ManagementUnitException e) {
			e.printStackTrace();
		}
		//初始化检修范围
//		ManagementUnitDict unit1 = unitM.getRootUnit(ManagementUnitEnum.SUBCOMPANY.getType());
//		List<ManagementUnitDict> rank = unitM.getUnitsByPid(unit1.getId());
		List<ManagementUnitDict> depts1;
		try {
			depts1 = unitM.getAllMaintenanceScopes();
			for (ManagementUnitDict i : depts1) {
				rankList.add(i.getName());
			}
		} catch (ManagementUnitException e) {
			e.printStackTrace();
		}
	}

	public void initApplicantCompanys() {
		List<OverhaulMsg> msgs = ma.findOverhaulGroupByApplicantCompany();
		for (int i = 0; i < msgs.size(); i++) {
			if (msgs.get(i).getApplicantCompany() != null) {
				applicantCompanys.add(msgs.get(i).getApplicantCompany());
			}
		}
		for (String r : applicantCompanys) {
			String key = PingYinUtil.getPingYinHeader(r);
			List<String> ss = applicantCompanysMap.get(key);
			if (ss == null) {
				ss = new ArrayList<>();
				applicantCompanysMap.put(key, ss);
			}
			ss.add(r);
		}
	}

	public List<String> completeApplicantCompany(String query) {
		HashSet<String> set = new HashSet<String>();
		for (String s : applicantCompanysMap.keySet()) {
			if (s.startsWith(query))
				set.addAll(applicantCompanysMap.get(s));
		}
		for (String s : applicantCompanys) {
			if (s.contains(query)) {
				set.add(s);
			}
		}
		List<String> results = new ArrayList<String>();
		results.addAll(set);
		return results;
	}

	public List<String> completeWorkplace(String query) {
		HashSet<String> set = new HashSet<String>();
		for (String s : cimstation.keySet()) {
			if (s.startsWith(query))
				set.addAll(cimstation.get(s));
		}
		for (Substation s : substations) {
			if (s.getName() != null && s.getName().contains(query)) {
				set.add(s.getName());
			}
		}
		List<String> res = new ArrayList<>();
		res.addAll(set);
		return res;
	}

	public List<String> completeSubControlArea(String query) {
		HashSet<String> set = new HashSet<String>();
		for (String s : subareas) {
			if (s != null && s.contains(query)) {
				set.add(s);
			}
		}
		List<String> res = new ArrayList<>();
		res.addAll(set);
		return res;
	}
	
	public List<String> completeRank(String query) {
		HashSet<String> set = new HashSet<String>();
		for (String s : rankList) {
			if (s != null && s.contains(query)) {
				set.add(s);
			}
		}
		List<String> res = new ArrayList<>();
		res.addAll(set);
		return res;
	}

	public Map<String, List<String>> getApplicantCompanysMap() {
		return applicantCompanysMap;
	}

	public void setApplicantCompanysMap(Map<String, List<String>> applicantCompanysMap) {
		this.applicantCompanysMap = applicantCompanysMap;
	}

	public Map<String, List<String>> getCimstation() {
		return cimstation;
	}

	public void setCimstation(Map<String, List<String>> cimstation) {
		this.cimstation = cimstation;
	}

	public List<Substation> getSubstations() {
		return substations;
	}

	public void setSubstations(List<Substation> substations) {
		this.substations = substations;
	}

	public List<String> getApplicantCompanys() {
		return applicantCompanys;
	}

	public void setApplicantCompanys(List<String> applicantCompanys) {
		this.applicantCompanys = applicantCompanys;
	}

	public List<String> getSubareas() {
		return subareas;
	}

	public void setSubareas(List<String> subareas) {
		this.subareas = subareas;
	}

}
